{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "operational-monday",
   "metadata": {},
   "source": [
    "# Problem 14.14 Refrigeration Cycle Calculation Using the Peng-Robinson EOS"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "engaged-green",
   "metadata": {},
   "source": [
    "A refrigerator uses the refrigerant R-12, dichlorodifluoromethane. The steps and conditions of the cycle are as follows:\n",
    "    \n",
    "* Isobaric condensation to saturation temperature 30°C\n",
    "* Adiabatic  let-down to P2 = 20 degrees subcooling\n",
    "* Isobaric evaporation to saturation temperature of 20 °C\n",
    "* Isentropic compression to P4 = 30 °C\n",
    "\n",
    "Use the Peng-Robinson EOS."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "optional-binary",
   "metadata": {},
   "source": [
    "## Solution\n",
    "\n",
    "This is quite straightforward, with the only complication coming from the degrees of subcooling."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "sweet-teacher",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-17303.632306857875, 13889.615500762327, 3414.0168060955466)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Set the conditions and imports\n",
    "from scipy.constants import bar\n",
    "from thermo import ChemicalConstantsPackage, PRMIX, CEOSLiquid, CEOSGas, FlashPureVLS\n",
    "fluid = 'dichlorodifluoromethane'\n",
    "constants, correlations = ChemicalConstantsPackage.from_IDs([fluid])\n",
    "\n",
    "zs = [1]\n",
    "\n",
    "eos_kwargs = dict(Tcs=constants.Tcs, Pcs=constants.Pcs, omegas=constants.omegas)\n",
    "liquid = CEOSLiquid(PRMIX, HeatCapacityGases=correlations.HeatCapacityGases,\n",
    "                    eos_kwargs=eos_kwargs)\n",
    "gas = CEOSGas(PRMIX, HeatCapacityGases=correlations.HeatCapacityGases, \n",
    "              eos_kwargs=eos_kwargs)\n",
    "flasher = FlashPureVLS(constants, correlations, liquids=[liquid], gas=gas, solids=[])\n",
    "\n",
    "T1 = 273.15+30\n",
    "state_1 = flasher.flash(VF=0, T=T1)\n",
    "saturation_state_1 = flasher.flash(T=-20+273.15, VF=1)\n",
    "# Wording is unclear for state 2 but thermodynamically his is what makes sense\n",
    "state_2 = flasher.flash(H=state_1.H(), P=saturation_state_1.P)\n",
    "# Check the flash lowers the pressure\n",
    "assert state_2.P < state_1.P\n",
    "state_3 = flasher.flash(P=state_2.P, VF=1)\n",
    "saturation_state_2 = flasher.flash(T=30+273.15, VF=1)\n",
    "state_4 = flasher.flash(P=saturation_state_2.P, S=state_3.S())\n",
    "states = [state_1, state_2, state_3, state_4]\n",
    "\n",
    "condensation_duty = (state_1.H() - state_4.H())\n",
    "heating_duty = state_3.H() - state_2.H()\n",
    "compressing_duty = state_4.H() - state_3.H()\n",
    "condensation_duty, heating_duty, compressing_duty"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "strong-composition",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-9.094947017729282e-13"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check the cycle convergence\n",
    "cycle_error = sum([condensation_duty, heating_duty, compressing_duty])\n",
    "cycle_error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "emotional-ready",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "T=303.15 K, P=742091.64 Pa, VF=0.00, S=-72.41 J/(mol*K), H=-17294.43 J/(mol)\n",
      "T=253.15 K, P=150606.23 Pa, VF=0.29, S=-70.23 J/(mol*K), H=-17294.43 J/(mol)\n",
      "T=253.15 K, P=150606.23 Pa, VF=1.00, S=-15.37 J/(mol*K), H=-3404.81 J/(mol)\n",
      "T=311.88 K, P=742091.64 Pa, VF=1.00, S=-15.37 J/(mol*K), H=9.20 J/(mol)\n"
     ]
    }
   ],
   "source": [
    "for state in states:\n",
    "    print(f'T={state.T:.2f} K, P={state.P:.2f} Pa, VF={state.VF:.2f}, S={state.S():.2f} J/(mol*K), H={state.H():.2f} J/(mol)') "
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
